#ifndef DESIGNERWORKBENCH_H
#define DESIGNERWORKBENCH_H

#include <QtGui/QMenu>
#include <QtGui/QMenuBar>

#include <QtCore/QObject>
#include <QtCore/QHash>
#include <QtCore/QSet>
#include <QtCore/QList>
#include <QtCore/QRect>

class QDesignerActions;
class QDesignerToolWindow;
class QDesignerFormWindow;
class DockedMainWindow;
class Project;
class Settings;
class QProcess;
//class ToolBarManager;

class QDesignerFormWindowInterface;
class QDesignerFormEditorInterface;
class QDesignerFormWindowManagerInterface;

namespace qdesigner_internal {
class QDesignerIntegration;
}

class QDesignerWorkbench : public QObject
{
    Q_OBJECT
public:    
    virtual ~QDesignerWorkbench();
    static QDesignerWorkbench* instance();
    static void destroy();

    QDesignerFormWindow *openForm(const QString &fileName, QString *errorMessage);
    QDesignerFormWindow *openTemplate(const QString &templateFileName,
                                      const QString &editorFileName,
                                      QString *errorMessage);

    int formWindowCount() const;
    QDesignerFormWindow *formWindow(int index) const;

    DockedMainWindow *mainWindow() const {return m_dockedMainWindow;}

    QDesignerFormEditorInterface *core() const { return m_core; }
    QDesignerActions *actionManager() const { return m_actionManager; }

    QRect availableGeometry() const;
    QRect desktopGeometry() const;

    int marginHint() const {return 20;}

    bool handleClose();
    bool readInBackup();
    void updateBackup(QDesignerFormWindowInterface* fwi);
    bool saveForm(QDesignerFormWindowInterface *fw);

    bool openProject(const QString & projectFile);
    bool saveProject();
    bool closeProject();
    Project* project() const { return m_project; }

signals:
    void initialized();

public slots:
    void addFormWindow(QDesignerFormWindow *formWindow);
    void removeFormWindow(QDesignerFormWindow *formWindow);
    void toggleFormMinimizationState();
    void navigateToSlot(const QString &objectName, const QString &signalSignature, const QStringList &parameterNames);
    void projectSettingsSlot();
    void projectDatabaseSlot();
    void editProjecScriptSlot();
    void runProjectSlot();

private slots:
    void handleCloseEvent(QCloseEvent *);
    void initializeCorePlugins();
    void formWindowActionTriggered(QAction *a);
    void minimizationStateChanged(QDesignerFormWindowInterface *formWindow, bool minimized);
    void slotFormWindowActivated(QDesignerFormWindow* fw);

private:
    QDesignerWorkbench(); // Singleton, look for instance()
    QDesignerFormWindowManagerInterface *formWindowManager() const;
    QDesignerToolWindow *widgetBoxToolWindow() const;  
    void saveSettings() const;
    QDesignerFormWindow *loadForm(const QString &fileName, bool detectLineTermiantorMode, bool *uic3Converted, QString *errorMessage);
    void resizeForm(QDesignerFormWindow *fw,  const QWidget *mainContainer) const;

    bool isFormWindowMinimized(const QDesignerFormWindow *fw);
    void setFormWindowMinimized(QDesignerFormWindow *fw, bool minimized);

    DockedMainWindow *m_dockedMainWindow;

    QList<QDesignerToolWindow*> m_toolWindows;
    QList<QDesignerFormWindow*> m_formWindows;

    QDesignerActions *m_actionManager;
    QActionGroup *m_windowActions;

    QMenu *m_toolbarMenu;
    QMenu *m_windowMenu;
    QMenu *m_projectMenu;
    QMenuBar *m_globalMenuBar;

    static QDesignerWorkbench *m_self;
    QDesignerFormEditorInterface *m_core;
    qdesigner_internal::QDesignerIntegration *m_integration;

    Project *m_project;
    Settings *m_settings;
    QProcess *m_scripterProcess;
    QDesignerFormWindow* m_activeFormWindow;

    friend class QDesignerActions;
};

#endif // DESIGNERWORKBENCH_H
